package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageInfo;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.query.ShopQuery;
import cn.itsource.pethome.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理所有的接口",description = "店铺接口详细描述")
public class ShopController {


    @Autowired
    private IShopService shopService;



    @PatchMapping
    @ApiOperation(value = "查询所有的店铺",notes = "这个接口没有分页")
    public PageInfo<Shop> findPageByQuery(@RequestBody ShopQuery shopQuery){
        return shopService.findPageByQuery(shopQuery);
    }


    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }



    //新增和修改
    @PostMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody Shop shop){
        try {
            shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //批量删除
    @PatchMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Shop> shops){
        try {
            shopService.batchRemove(shops);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //根据id删除数据
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

   //商家入驻
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            //商家入驻
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


}
